function MatrixStack(){}

MatrixStack.prototype = {
    // 初期化
    init: function(){
        this.array.length = 0;
    },
    // topのmatrixの参照
    top: function(){
        return this.array[this.array.length-1];
    },
    // push (一つ前のmatrixに掛ける)
    push: function(m){
        var newMatrix;
        if (this.array.length > 0){
            newMatrix = this.top().x(m);
        } else {
            newMatrix = m;
        }
        this.array.push(newMatrix); 
        return newMatrix;
    },
    // pop 
    pop: function(){
        if (this.array.length <= 0){
            throw "Invalid Matrix";
        }
        var m = this.array.pop();
        return m;
    }
}

MatrixStack.create = function(){
    var ms = new MatrixStack();
    ms.array = [];
    return ms;
}
